<?php
/**
 * @package se-timeline
 * @version 1.0
 */
/*
Plugin Name: se-timeline
Plugin URI: Http://www.cnblogs.com/fxmbz/4059745.HTML
Description: 时间轴插件
Author: zhoubiwen
Version: 1.0
Author URI: http://www.cnblogs.com/fxmbz
*/

/*****************创建新的文章类型 */
//注册自定义文章类型-timees
/**
 * Register a custom post type called "timees".
 *
 * @see get_post_type_labels() for label keys.
 */
add_action('init', 'my_custom_post_timees');



add_action('admin_enqueue_scripts', function () {
    wp_enqueue_style('wd-woo-plugin-vs-style', plugins_url('/assets/css/backend.css'), array(), '1.1');
	wp_enqueue_script('wd-woo-plugin-vs-script-jquery', plugins_url('/assets/js/jquery.min.js'), array(), '1.1', true);
    wp_enqueue_script('wd-woo-plugin-vs-script', plugins_url('/assets/js/backend.js'), array('jquery'), '1.1', true);
    wp_localize_script('wd-woo-plugin-vs-script', 'wdwooplugin', array(
        'ajax_url' => admin_url('admin-ajax.php'),
		'z_curId'  => get_current_user_id(),
		'gongkaises' => get_post_meta(get_the_ID(), '_gongkai_value', true )
    ));
});

function my_custom_post_timees()
{
	$labels = array(
		'name'               => _x('时间线', 'post type 名称'),
		'singular_name'      => _x('timees', 'post type 单个 item 时的名称，因为英文有复数'),
		'add_new'            => _x('新建时间线', '添加新内容的链接名称'),
		'add_new_item'       => __('新建时间线'),
		'edit_item'          => __('编辑时间线'),
		'new_item'           => __('新时间线'),
		'all_items'          => __('所有时间线'),
		'view_item'          => __('查看时间线'),
		'search_items'       => __('搜索时间线'),
		'not_found'          => __('没有找到有关时间线'),
		'not_found_in_trash' => __('回收站里面没有相关时间线'),
		'parent_item_colon'  => '',
		'menu_name'          => '时间线'
	);
	$args = array(
		'labels'        => $labels,
		'description'   => '我们网站的时间线信息',
		'public'        => true,
		'menu_position' => 5,
		'supports'      => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
		'has_archive'   => true
	);
	register_post_type('timees', $args);
}


//注册自定义文章类型-添加分类
function my_taxonomies_timees()
{
	$labels = array(
		'name'              => _x('时间线分类', 'taxonomy 名称'),
		'singular_name'     => _x('时间线分类', 'taxonomy 单数名称'),
		'search_items'      => __('搜索时间线分类'),
		'all_items'         => __('所有时间线分类'),
		'parent_item'       => __('该时间线分类的上级分类'),
		'parent_item_colon' => __('该时间线分类的上级分类：'),
		'edit_item'         => __('编辑时间线分类'),
		'update_item'       => __('更新时间线分类'),
		'add_new_item'      => __('添加新的时间线分类'),
		'new_item_name'     => __('新时间线分类'),
		'menu_name'         => __('时间线分类'),
	);
	$args = array(
		'labels' => $labels,
		'hierarchical' => true,
		'show_ui'           => true,
		'show_in_menu'            => true,
		'show_in_nav_menus'       => true
	);
	register_taxonomy('timees_category', 'timees', $args);
}
add_action('init', 'my_taxonomies_timees', 0);


//注册自定义文章类型-添加标签
add_action('init', 'create_timees_taxonomies', 0);
function create_timees_taxonomies()
{
	$labels = array(
		'name'              => '时间线标签', 'taxonomy general name',
		'singular_name'     => '时间线标签', 'taxonomy singular name',
		'search_items'      => '搜索时间线标签',
		'all_items'         => '所有时间线标签',
		'parent_item'       => 'Parent Genre',
		'parent_item_colon' => 'Parent Genre:',
		'edit_item'         => '编辑时间线标签',
		'update_item'       => '更新时间线标签',
		'add_new_item'      => '添加新时间线标签',
		'new_item_name'     => 'New Genre Name',
		'menu_name'         => '时间线标签',
	);
	$args = array(
		'hierarchical'      => false,
		'labels'            => $labels,
		'show_ui'           => true,
		'show_admin_column' => true,
		'query_var'         => true,
		// 'rewrite'           => array( 'slug' => lawyer ),
	);
	register_taxonomy('timees_tag', 'timees', $args);
}



/*****************创建新的文章类型 */

/*******************创建meta-box元框 */
//创建自定义字段meta-box 元框 和 元数据
$new_meta_boxes = array(
	"description" => array(
		"name" => "_timees",
		"std" => "排序时间自段",
		"title" => "时间排序"
	),
	"projets" => array(
		"name" => "_projets",
		"std" => "关联项目",
		"title" => "关联项目"
	),
	"quanzhong" => array(
		"name" => "_quanzhong",
		"std" => "权重",
		"title" => "权重"
	),
	"gongkai" => array(
		"name" => "_gongkai",
		"std" => "是否公开",
		"title" => "是否公开"
	),
	"useres" => array(
		"name" => "_useres",
		"std" => "选择权限用户",
		"title" => "选择权限用户"
	),
	"isok" => array(
		"name" => "_isok",
		"std" => "是否完成",
		"title" => "是否完成"
	)
);
function new_meta_boxes()
{
	global $post, $new_meta_boxes,$wpdb;
	
	$table_name = $wpdb->prefix . "users";
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');    
	foreach ($new_meta_boxes as $meta_box) {
		$meta_box_value = get_post_meta($post->ID, $meta_box['name'] . '_value', true);
		if ($meta_box['name'] == "_timees") {
			// 自定义字段标题
			echo '<h3>' . $meta_box['title'] . '</h3>';
			// 自定义字段输入框
			echo '<input name="' . $meta_box['name'] . '_value" value="' . $meta_box_value . '" type="datetime-local" >';
		} else if($meta_box['name'] == "_projets"){
			//获取自定义分类文章
			$args = array(
				'posts_per_page' => 5,
				'orderby' => 'post_date',
				'post_type' => 'projectes'
			);
			$postslist = get_posts($args);
			// 自定义字段输入框
			$strs = '<select name="' . $meta_box['name'] . '_value"><option value="">请选择</option>';
			foreach ($postslist as $postess) {
				$gr_seleted = "";
				if ($postess->ID == (int)$meta_box_value) {
					$gr_seleted = "selected";
				}
				$strs .= '<option value="' . $postess->ID . '"  ' . $gr_seleted . '>' . $postess->post_title . '</option>';
			}
			$strs .= '</select>';
			// 自定义字段标题
			echo '<h3>' . $meta_box['title'] . '</h3>';
			echo $strs;
		}else if($meta_box['name'] == "_quanzhong"){
			$qz = array(
				array(
					'v'=>'非紧急',
					'i'=>1
				),
				array(
					'v'=>'紧急',
					'i'=>2
				)
			);
			$quanzhong = '<select name="' . $meta_box['name'] . '_value"><option value="">请选择</option>'; 
			foreach($qz as $v){
				$qz_seleted = "";
				if ($v['i'] == (int)$meta_box_value) {
					$qz_seleted = "selected";
				}
				$quanzhong .= '<option value="'.$v['i'].'" '.$qz_seleted.'>'.$v['v'].'</option>';
			}
			$quanzhong .= '</select>';
			echo '<h3>' . $meta_box['title'] . '</h3>';
			echo $quanzhong;
		}else if($meta_box['name'] == "_gongkai"){
			$gk = array(
				array(
					'v'=>'公开',
					'i'=>1
				),
				array(
					'v'=>'私有',
					'i'=>2
				)
			);
			$gongkai = '<select name="' . $meta_box['name'] . '_value" id="selectgk"><option value="">请选择</option>'; 
			foreach($gk as $v){
				$gk_seleted = "";
				if ($v['i'] == (int)$meta_box_value) {
					$gk_seleted = "selected";
				}
				$gongkai .= '<option value="'.$v['i'].'" '.$gk_seleted.'>'.$v['v'].'</option>';
			}
			$gongkai .= '</select>';
			echo '<h3>' . $meta_box['title'] . '</h3>';
			echo $gongkai;
		}else if($meta_box['name'] == "_useres"){
			$current_users = get_current_user_id();
			if(get_post_meta(get_the_ID(), '_gongkai_value', true )==1){
				$cureArra= array();
			}else{
				$cureArra= array($current_users);
			}
			
			$valuslist = array();
			if($meta_box_value == '' || empty($meta_box_value)){
				$valuslist = $cureArra;
			}else{
				$valuslist = $meta_box_value;
			}
			$userlist = $wpdb->get_results("SELECT `ID`,user_login,user_nicename FROM $table_name");
			echo '<h3>' . $meta_box['title'] . '</h3>';
			foreach($userlist as $vv){
				if(!empty($valuslist) && in_array($vv->ID,$valuslist)){
					$vv->checkeds = 'checked';
				}else{
					$vv->checkeds = ''; 
				}
			}
			foreach($userlist as $vv){
				echo  '<div style="display: inline-flex; flex-direction: row; justify-content: flex-start; align-items:center;" id="userlistId">';
				echo  '<label><input type="checkbox" value="'.$vv->ID.'" '.$vv->checkeds.'  name="'.$meta_box['name'].'_value[]'.'" />'.$vv->user_nicename.'</label>';
				echo  '</div>';
			}
		}else if($meta_box['name'] == "_isok"){
			$gkisok = array(
				array(
					'v'=>'未完成',
					'i'=>1
				),
				array(
					'v'=>'已完成',
					'i'=>2
				)
			);
			$isok = '<select name="' . $meta_box['name'] . '_value"><option value="">请选择</option>'; 
			foreach($gkisok as $v){
				$ok_seleted = "";
				if ($v['i'] == (int)$meta_box_value) {
					$ok_seleted = "selected";
				}
				$isok .= '<option value="'.$v['i'].'" '.$ok_seleted.'>'.$v['v'].'</option>';
			}
			$isok .= '</select>';
			echo '<h3>' . $meta_box['title'] . '</h3>';
			echo $isok;
		}
	}
	echo '<input type="hidden" name="ludou_metaboxes_nonce" id="ludou_metaboxes_nonce" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
}

//创建自定义字段模块
function create_meta_box()
{
	if (function_exists('add_meta_box')) {
		add_meta_box('new-meta-boxes', '自定义模块', 'new_meta_boxes', 'timees', 'normal', 'high');
	}
}

//保存文章数据
function save_postdata($post_id)
{
	global $new_meta_boxes;
	if (!wp_verify_nonce($_POST['ludou_metaboxes_nonce'], plugin_basename(__FILE__)))
		return;
	if (!current_user_can('edit_posts', $post_id))
		return;
	foreach ($new_meta_boxes as $meta_box) {
		$data = $_POST[$meta_box['name'] . '_value'];
		if ($data == "")
			delete_post_meta($post_id, $meta_box['name'] . '_value', get_post_meta($post_id, $meta_box['name'] . '_value', true));
		else
			update_post_meta($post_id, $meta_box['name'] . '_value', $data);
	}
}

//将函数连接到指定action（动作）
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
/*******************创建meta-box元框 */



//注册自定义文章类型-projectes
/**
 * Register a custom post type called "projectes".
 *
 * @see get_post_type_labels() for label keys.
 */
add_action('init', 'my_custom_post_projectes');
function my_custom_post_projectes()
{
	$labels = array(
		'name'               => _x('项目', 'post type 名称'),
		'singular_name'      => _x('projectes', 'post type 单个 item 时的名称，因为英文有复数'),
		'add_new'            => _x('新建项目', '添加新内容的链接名称'),
		'add_new_item'       => __('新建项目'),
		'edit_item'          => __('编辑项目'),
		'new_item'           => __('新项目'),
		'all_items'          => __('所有项目'),
		'view_item'          => __('查看项目'),
		'search_items'       => __('搜索项目'),
		'not_found'          => __('没有找到有关项目'),
		'not_found_in_trash' => __('回收站里面没有相关项目'),
		'parent_item_colon'  => '',
		'menu_name'          => '项目'
	);
	$args = array(
		'labels'        => $labels,
		'description'   => '我们网站的项目信息',
		'public'        => true,
		'menu_position' => 5,
		'supports'      => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
		'has_archive'   => true
	);
	register_post_type('projectes', $args);
}


//注册自定义文章类型-添加分类
function my_taxonomies_projectes()
{
	$labels = array(
		'name'              => _x('项目分类', 'taxonomy 名称'),
		'singular_name'     => _x('项目分类', 'taxonomy 单数名称'),
		'search_items'      => __('搜索项目分类'),
		'all_items'         => __('所有项目分类'),
		'parent_item'       => __('该项目分类的上级分类'),
		'parent_item_colon' => __('该项目分类的上级分类：'),
		'edit_item'         => __('编辑项目分类'),
		'update_item'       => __('更新项目分类'),
		'add_new_item'      => __('添加新的项目分类'),
		'new_item_name'     => __('新项目分类'),
		'menu_name'         => __('项目分类'),
	);
	$args = array(
		'labels' => $labels,
		'hierarchical' => true,
		'show_ui'           => true,
		'show_in_menu'            => true,
		'show_in_nav_menus'       => true
	);
	register_taxonomy('projectes_category', 'projectes', $args);
}
add_action('init', 'my_taxonomies_projectes', 0);


//注册自定义文章类型-添加标签
add_action('init', 'create_projectes_taxonomies', 0);
function create_projectes_taxonomies()
{
	$labels = array(
		'name'              => '项目标签', 'taxonomy general name',
		'singular_name'     => '项目标签', 'taxonomy singular name',
		'search_items'      => '搜索项目标签',
		'all_items'         => '所有项目标签',
		'parent_item'       => 'Parent Genre',
		'parent_item_colon' => 'Parent Genre:',
		'edit_item'         => '编辑项目标签',
		'update_item'       => '更新项目标签',
		'add_new_item'      => '添加新项目标签',
		'new_item_name'     => 'New Genre Name',
		'menu_name'         => '项目标签',
	);
	$args = array(
		'hierarchical'      => false,
		'labels'            => $labels,
		'show_ui'           => true,
		'show_admin_column' => true,
		'query_var'         => true,
		// 'rewrite'           => array( 'slug' => lawyer ),
	);
	register_taxonomy('projectes_tag', 'projectes', $args);
}


//项目页添加自定义面板
//创建自定义字段meta-box 元框 和 元数据
$new_meta_boxes_pro = array(
	"description" => array(
		"name" => "_jindu",
		"std" => "项目进度",
		"title" => "项目进度"
	)
);
function new_meta_boxes_pro()
{
	global $post, $new_meta_boxes_pro;

	foreach ($new_meta_boxes_pro as $meta_box) {
		$meta_box_value = get_post_meta($post->ID, $meta_box['name'] . '_value', true);
		if ($meta_box_value == "")
			$meta_box_value = $meta_box['std'];
		if ($meta_box['name'] == "_jindu") {
			// 自定义字段标题
			echo '<h3>' . $meta_box['title'] . '</h3>';
			// 自定义字段输入框
			echo '<input name="' . $meta_box['name'] . '_value" value="' . $meta_box_value . '" type="number" >%';
		}
	}
	echo '<input type="hidden" name="ludou_metaboxes_nonce" id="ludou_metaboxes_nonce" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
}

//创建自定义字段模块
function create_meta_box_pro()
{
	if (function_exists('add_meta_box')) {
		add_meta_box('new-meta-boxes', '自定义模块', 'new_meta_boxes_pro', 'projectes', 'normal', 'high');
	}
}

//保存文章数据
function save_postdata_pro($post_id)
{
	global $new_meta_boxes_pro;
	if (!wp_verify_nonce($_POST['ludou_metaboxes_nonce'], plugin_basename(__FILE__)))
		return;
	if (!current_user_can('edit_posts', $post_id))
		return;
	foreach ($new_meta_boxes_pro as $meta_box) {
		$data = $_POST[$meta_box['name'] . '_value'];
		if ($data == "")
			delete_post_meta($post_id, $meta_box['name'] . '_value', get_post_meta($post_id, $meta_box['name'] . '_value', true));
		else
			update_post_meta($post_id, $meta_box['name'] . '_value', $data);
	}
}

//将函数连接到指定action（动作）
add_action('admin_menu', 'create_meta_box_pro');
add_action('save_post', 'save_postdata_pro');
/*******************创建meta-box元框 */